package com.dgut.shopManage.entity.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.dgut.common.util.Xtype;
import com.dgut.shopManage.entity.Apply;
import com.fasterxml.jackson.annotation.JsonFormat;

public class ApplyQueryDTO {

	private Xtype type;				//经营类型
	private Date sTime;    				//租用开始时间
	private Date eTime;					//租用结束时间
	private Date aTime;					//申请时间
	private String applyRent;
	
	
	public Xtype getType() {
		return type;
	}
	public void setType(Xtype type) {
		this.type = type;
	}

	public Date getsTime() {
		return sTime;
	}
	public void setsTime(Date sTime) {
		this.sTime = sTime;
	}
	public Date geteTime() {
		return eTime;
	}
	public void seteTime(Date eTime) {
		this.eTime = eTime;
	}
	public Date getaTime() {
		return aTime;
	}
	public void setaTime(Date aTime) {
		this.aTime = aTime;
	}
	public String getApplyRent() {
		return applyRent;
	}
	public void setApplyRent(String applyRent) {
		this.applyRent = applyRent;
	}

	public static Specification<Apply> getSpecification(ApplyQueryDTO applyQueryDTO)
	{
		Specification<Apply> spec = new Specification<Apply>() {
			public Predicate toPredicate(Root<Apply> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				
				 if(null != applyQueryDTO && !StringUtils.isEmpty(applyQueryDTO.getType())) {
					 Predicate  p1 =  cb.equal(root.get("type").as(String.class),applyQueryDTO.getType());
					 list.add(p1);
				 }
			
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
}
